package com.wancaijinfu.component;

import com.wancaijinfu.actitvity.R;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.res.TypedArray;
import android.text.Editable;
import android.text.InputFilter;
import android.text.InputType;
import android.text.TextWatcher;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.inputmethod.EditorInfo;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;

public class MEditText extends LinearLayout {

	private ImageView headerView;
	public EditText contentView;
	private ImageView closeView;

	public MEditText(Context context) {
		this(context, null);
	}

	@SuppressLint("NewApi")
	public MEditText(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
		initView(attrs);
		initListener();
	}

	public MEditText(Context context, AttributeSet attrs) {
		this(context, attrs, 0);
	}

	@Override
	protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
		super.onMeasure(widthMeasureSpec, heightMeasureSpec);
	}

	private void initView(AttributeSet attrs) {
		View view = LayoutInflater.from(getContext()).inflate(
				R.layout.medittext_content, this, false);
		contentView = (EditText) view.findViewById(R.id.medittext_content);
		closeView = (ImageView) view
				.findViewById(R.id.medittext_close_imageview);

		TypedArray array = getContext().obtainStyledAttributes(attrs,
				R.styleable.MEditText);
		for (int i = 0; i < array.getIndexCount(); i++) {
			int attr = array.getIndex(i);
			switch (attr) {
			case R.styleable.MEditText_hint:
				String hint = array.getString(attr);
				contentView.setHint(hint);
				break;
			case R.styleable.MEditText_password:
				contentView.setInputType(0x81);
				break;
			case R.styleable.MEditText_phoneNumber:
				contentView.setInputType(InputType.TYPE_CLASS_PHONE);
				break;
			case R.styleable.MEditText_maxLength:
				contentView
						.setFilters(new InputFilter[] { new InputFilter.LengthFilter(
								array.getInt(attr, Integer.MAX_VALUE)) });
				break;
			case R.styleable.MEditText_imeOptions:
				String options = array.getString(attr);
				
				if (options.equals("next")) {
					contentView.setImeOptions(EditorInfo.IME_ACTION_NEXT);
				}
				else if (options.equals("done")) {
					contentView.setImeOptions(EditorInfo.IME_ACTION_DONE);
				}
				break;
			case R.styleable.MEditText_height:
				int height = (int) array.getDimension(attr, -1);
				if (height == -1) break;
				View linearLayout = view.findViewById(R.id.medittext_layout);
				ViewGroup.LayoutParams lp = linearLayout.getLayoutParams();
				lp.height = height;
				linearLayout.setLayoutParams(lp);
				break;
			}
		}

		addView(view);
	}

	public String getText() {
		return contentView.getText().toString();
	}
	
	public void setText(String text) {
		contentView.setText(text);
	}
	
	public void clearText() {
		contentView.setText("");
	}
	
	public void setHint(String hint) {
		contentView.setHint(hint);
	}

	private void initListener() {
		contentView.addTextChangedListener(new TextWatcher() {
			@Override
			public void onTextChanged(CharSequence s, int start, int before,
					int count) {
				String content = getText();
				if (content.equals("") || content == null) {
					closeView.setVisibility(View.INVISIBLE);
				} else {
					closeView.setVisibility(View.VISIBLE);
				}
			}

			@Override
			public void beforeTextChanged(CharSequence s, int start, int count,
					int after) {

			}

			@Override
			public void afterTextChanged(Editable s) {
				// TODO Auto-generated method stub

			}
		});
		closeView.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				// Toast.makeText(getContext(), getText(),
				// Toast.LENGTH_SHORT).show();
				contentView.setText("");
			}
		});
	}
	
	public void setDiabled() {
		contentView.setEnabled(false);
		closeView.setVisibility(View.GONE);
	}

}
